<?php include_once ('function/check_login_function.php'); onlyValid(); onlyDoctor();?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Health Tracker</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Health Tracker">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <style>
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>
    <link href="css/bootstrap-responsive.css" rel="stylesheet">
    <link rel="shortcut icon" href="ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="ico/apple-touch-icon-57-precomposed.png">
  </head>

  <body>


<?php include_once('nav.php'); ?>
      
    <div class="container">
    <div class="row">
    <h1 style="padding-left: 20px;">Patients</h1>
    <div class="span8" style="padding-left:40px;">
      
				
				<?php 
					if (isset($_SESSION['message'])) {
						foreach ($_SESSION['message'] as $message) {
							echo '<div class="alert">'.constant($message) . '</div>';
						}
						unset($_SESSION['message']);
					} 
					
					if (isset($_POST['patientID'])) {
						echo '<a href="patients.php">Back</a> <br/><br/>';
						$patient_id = $_POST['patientID'];
						if ($user->isPatient($patient_id)) {
							$patient = Patient::findPatient($patient_id);
							echo '<h>' . User::findName($patient_id) . '</h>';
							echo '<table class="table table-striped" style="margin-left:20px;">';
							echo "<tr><td>E-mail:</td><td>". $patient['user_email'] . "</td></tr>";
							echo "<tr><td>Gender:</td><td>". $patient['user_gender'] . "</td></tr>";
								$address = explode('|', $patient['user_address']);
								$address1 = $address[0];
								$address2 = $address[1];
								$city = $address[2];
								$state = $address[3];
								$country = $address[4];
								$postal = $address[5];

								$display_address = "";
								if (strlen($address1) + strlen($address2) !== 0) {
									$display_address .= $address1 . ' '. $address2 . '<br />';
								}
								if (strlen($city) + strlen($state) !== 0) {
									$display_address .= $city . ' '. $state . '<br />';
								}
								if (strlen($country) + strlen($postal) !== 0) {
									$display_address .= $country . ' '. $postal . '<br />';
								}					
							echo "<tr><td>Address:</td><td>". $display_address  . "</td></tr>";
							echo "<tr><td>Contact:</td><td>". $patient['user_contact']  . "</td></tr>";
							echo "<tr><td>Date of Birth:</td><td>". $patient['user_dob']  . "</td></tr>";
							echo "<tr><td>Bloodtype:</td><td>". $patient['patient_bloodtype']  . "</td></tr>";
							echo "<tr><td>Allergy:</td><td>". $patient['patient_allergy']  . "</td></tr>";
							echo "</table>";
							
							//Data log of patient
							$datalog_list = DataLog::getDataLogList($patient_id);						
							echo '<h3 id="datalog">Data Log</h3>';
							if ($datalog_list !== null) { 
								echo '<table class="table">';
								echo '<tr>';
								echo '<th>Type</th>';
								echo '<th>Value</th>';
								echo '<th>Notes</th>';
								echo '<th>Date</th>';
								echo '<th></th>';
								echo '</tr>';
								
								foreach ($datalog_list as $datalog) {
									echo '<tr>';
									echo '<td>' . DataLog::findTypeName($datalog['datalog_type_id']) . '</td>';
									echo '<td>' . $datalog['datalog_value'] . '</td>';
									echo '<td>' . $datalog['datalog_note'] . '</td>';
									echo '<td>' . $datalog['created_at'] . '</td>';
									echo '</tr>';
								}
								echo '</table> <br />';
							}	else {
								echo constant("MSG_LOG_NONE");
							}			
							
							//Symptom of patient
							$symptom_list = Symptom::getSymptomList($patient_id);						
							echo '<h3 id="symptom">Symptom</h3>';
							if ($symptom_list !== null) { 
								echo '<table class="table">';
								echo '<tr>';
								echo '<th>Name</th>';
								echo '<th>Level</th>';
								echo '<th>Notes</th>';
								echo '<th>Date</th>';
								echo '<th></th>';
								echo '</tr>';
								
								foreach ($symptom_list as $symptom) {
									echo '<tr>';
									echo '<td>' . $symptom['symptom_name'] . '</td>';
									echo '<td>' . $symptom['symptom_level'] . '</td>';
									echo '<td>' . $symptom['symptom_note'] . '</td>';
									echo '<td>' . $symptom['created_at'] . '</td>';
									echo '</tr>';
								}
								echo '</table> <br />';
							}	else {
								echo constant("MSG_SYMPTOM_NONE");
							}		
							
							//Food diary of patient
							$food_list = Food::getFoodList($patient_id);						
							echo '<h3 id="food">Food Diary</h3>';
							if ($food_list !== null) { 
								echo '<table class="table">';
								echo '<tr>';
								echo '<th>Name</th>';
								echo '<th>Calories</th>';
								echo '<th>Notes</th>';
								echo '<th>Date</th>';
								echo '<th></th>';
								echo '</tr>';
								
								foreach ($food_list as $food) {
									echo '<tr>';
									echo '<td>' . $food['food_name'] . '</td>';
									echo '<td>' . $food['food_calories'] . '</td>';
									echo '<td>' . $food['food_note'] . '</td>';
									echo '<td>' . $food['created_at'] . '</td>';
									echo '</tr>';
								}
								echo '</table> <br />';
							}	else {
								echo constant("MSG_FOOD_NONE");
							}		
	
						} else {
							echo constant("MSG_PATIENT_NONEXISTENT"). '<br />';
						}
					} else {
				?>
					<p>List of patients</p>
					<table>
					<?php
						$patient_list = $user->getPatientList();
						if ($patient_list !== null) {
							$count = 1;
							foreach ($patient_list as $patient) {
								$patient_id = $patient['patient_id'];
								echo '<tr>';
								echo '<td> <b>' . $count++ . '. '. User::findName($patient_id);
								if (intval($patient['request_pending']) === 1) {
									echo ' (Pending) ';
								}
								echo '</b> </td>';
								echo '<td>';
								echo '<form action="patients.php" method="POST">';
								echo '<input type="hidden" id="patientID" name="patientID" value="' . $patient_id . '"/>';
								echo '<input type="hidden" id="from" name="from" value="patients"/>';
								echo '<input type="submit" name="viewPatient" class="btn btn-primary" value="View Patient" /> ';
								echo' </form> </td>';
								echo '<td> <form action="function/form_function.php" method="POST">';
								echo '<input type="hidden" id="patientID" name="patientID" value="' . $patient_id . '"/>';
								echo '<input type="hidden" id="from" name="from" value="patients.php"/>';
								if (intval($patient['request_pending']) === 1) {
									echo '<input type="submit" name="acceptPatientRequest" class="btn btn-primary" value="Accept Patient" /> ';
								}
								echo '<input type="submit" name="removePatientRequest" class="btn btn-primary" value="Remove Patient" /> ';
								echo' </form> </td>';
								echo '</tr>';
							}
						} else {
							echo constant("MSG_PATIENT_NONE");
						}
					?>
					</table>
				<?php } ?>
    </div>
    </div>
    </div> <!-- /container -->
  </body>
</html>